import { getMemberTagPage, deleteMemberTag } from '@/api/Member/index'
import { useConfirm } from '@/hooks'

const seachTableRef = ref(null)
export const useLabelManagement = () => {
  return {
    seachTableRef,
  }
}

export const useCurd = (props) => {
  // 表格数据
  const tableLoading = ref(false)
  const totalPage = ref(0)
  const tableData = ref([])

  // 搜索
  const handlerSearch = async (data) => {
    try {
      tableLoading.value = true
      const res = await getMemberTagPage(data)
      tableData.value = res.records
      totalPage.value = res.total
    } finally {
      tableLoading.value = false
    }
  }

  // 弹窗相关
  const dialogData = ref({})
  const showEditDialog = ref(false)
  const isEdit = ref(false)
  const closeEditDialog = () => {
    showEditDialog.value = false
    dialogData.value = {}
  }
  const openEditDialog = (row = {}) => {
    dialogData.value = row
    showEditDialog.value = true
  }

  const deleteLabel = async (row) => {
    const level = row.parentId === '0' ? '一级' : '二级'
    const tips = level === '一级' ? `确定删除【${row.labelName}】${level}标签？删除后该组下的二级标签将一并删除？` : `确定删除【${row.labelName}】${level}标签？`
    ElMessageBox.confirm(tips, '提示', {
      type: 'error',
      confirmButtonText:'删除',
      confirmButtonClass:'el-button--danger',
      beforeClose: async (action, instance, done) => {
        const res = await useConfirm(action, instance, done, deleteMemberTag, row.id)
        if (res) {
          ElMessage.success('删除成功')
          seachTableRef.value.handlerSearch()
        }
      }
    })
  }

  /**表格配置start */
  const btnLefts = [
    {
      label: '新增一级标签',
      type: 'primary',
      show: () => true,
      img: new URL("@/assets/icon/btn-add.png", import.meta.url),
      click: () => {
        isEdit.value = false
        openEditDialog()
      }
    },
  ]
  const btnRights = []
  const tableConfig = ref([
    {
      label: '一级标签名称',
      prop: 'labelName',
      format: (value, row) => row.parentId === '0' ? value : '',
    },
    {
      label: '二级标签名称',
      prop: 'labelName',
      format: (value, row) => row.parentId === '0' ? '' : value,
    },
    {
      type: 'slot',
      label: '标签人数',
      prop: 'labelNumber',
      slotName: 'tagNum'
    },
    {
      type: "action",
      label: '操作',
      prop: 'name',
      fixed: "right",
      btns: [
        {
          label: () => '新增',
          type: 'primary',
          show: (row) => row.parentId === '0',
          click: (row) => {
            isEdit.value = false
            openEditDialog(row)
          }
        },
        {
          label: () => '编辑',
          type: 'success',
          show: (row) => true,
          click: (row) => {
            isEdit.value = true
            openEditDialog(row)
          }
        },
        {
          label: () => '删除',
          type: 'danger',
          show: (row) => true,
          click: (row) => {
            deleteLabel(row)
          }
        }
      ]
    }
  ])
  const tableForms = ref([])
  /**表格配置end */

  return {
    tableLoading,
    totalPage,
    tableData,
    btnLefts,
    btnRights,
    tableConfig,
    tableForms,
    handlerSearch,
    showEditDialog,
    isEdit,
    dialogData,
    closeEditDialog,
    deleteLabel,
    openEditDialog
  }
}